/**
 * Copyright 2018-2020 stylefeng & fengshuonan (sn93@qq.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lcc.heart.core.base.warpper;

import com.baomidou.mybatisplus.plugins.Page;
import com.lcc.heart.core.util.LocalEntitysUtil;
import com.lcc.heart.kernel.model.page.PageResult;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 控制器查询结果的包装类基类
 *
 * @author liangcc
 * @date 2017年2月13日 下午10:49:36
 */
public abstract class BaseControllerWrapper {

    public Object obj = null;

    public BaseControllerWrapper(Object obj) {
        this.obj = obj;
    }

    @SuppressWarnings("unchecked")
    public <T> T wrap() {
        /**
         * 包装结果
         */
        Object object =  wrapNew();
        if (object != null) {
            return (T)object;
        }
        return null;
    }

    public Object wrapNew() {
        /**
         * 包装结果
         */
        if (this.obj instanceof List) {
            List list = (List) this.obj;
            if(list==null || list.size()==0){
                return null;
            }
            if (list.get(0) instanceof Map) {
                for (Object obj : list) {
                    wrapTheMap((Map<String, Object>)obj);
                }
            } else {
                List<Map<String, Object>> listnew = new ArrayList<Map<String, Object>>();
                for (Object obj : list) {
                    Map<String, Object> objMap = LocalEntitysUtil.entityToMap(obj);
                    wrapTheMap(objMap);
                    listnew.add(objMap);
                }
                return listnew;
            }
            return list;
        } else if (this.obj instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) this.obj;
            wrapTheMap(map);
            return map;
        } else if (this.obj instanceof Page) {
            Page<Map<String, Object>> page = (Page<Map<String, Object>>)this.obj;
            if (page.getRecords() != null) {
                return  page;
            }
        }  else if (this.obj instanceof PageResult) {
            PageResult<Map<String, Object>> pageResult = (PageResult<Map<String, Object>>)this.obj;
            if ( pageResult.getRows() != null) {
                return  pageResult;
            }
        }  else {
            Map<String, Object> objMap = LocalEntitysUtil.entityToMap(this.obj);
            wrapTheMap(objMap);
            return objMap;
        }
        return null;
    }

    protected abstract void wrapTheMap(Map<String, Object> map);

}
